package rootpw;

import javax.swing.SwingUtilities;

//
// Handler for uncaught exceptions.
// Credit: http://stuffthathappens.com/blog/2007/10/07/programmers-notebook-uncaught-exception-handlers/
final class MyExceptionHandler implements Thread.UncaughtExceptionHandler {

    public void uncaughtException(final Thread t, final Throwable e) {
        if (SwingUtilities.isEventDispatchThread()) {
            showException(t, e);
        } else {
            SwingUtilities.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    showException(t, e);
                }
            });
        }
    }

    private void showException(Thread t, Throwable e) 
    {
        String msg = String.format("Unexpected problem on thread %s: %s", t.getName(), e.getMessage());

        System.out.println(msg);
        System.exit(1);
    }
}
